package com.showapi.app.controller

import com.alibaba.fastjson.JSON
import com.showapi.app.service.DynamicService
import com.showapi.app.service.FlowService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController


@RestController
class DispatcherController {

    @Autowired @Qualifier("dynamicService")
    DynamicService dynamicService

    @Autowired @Qualifier("flowService")
    FlowService flowService

    @RequestMapping(value = ["/handler/{handler}/{action}","/handler/{handler}"])
    String handler(@PathVariable(name = "handler") String handler,
                   @PathVariable(required = false) String action,
                   @RequestParam Map<String,Object> params) {
        println params
        def ret=dynamicService.run_backgroud(handler,action,params)
        return JSON.toJSONString(ret)
    }


//    http://localhost:8080/handler_reload/handler.Tea
    @RequestMapping(value = [ "/handler_reload"])
    String handler_reload(@RequestParam(name = "classNameList", required = false) String classNameList) {
        println  classNameList
        def ret=dynamicService.reloadClass(classNameList )
        return JSON.toJSONString(ret)
    }


    @RequestMapping(value = ["/flow/{lineName}" ])
    String flow(@PathVariable(name = "lineName") String lineName,
                   @RequestParam Map<String,Object> params) {
        println params
        def ret=flowService.run_backgroud(lineName,params)
//        println ret
//        println "11111111111 "
        return JSON.toJSONString(ret)
    }

//    http://localhost:8080/flow_reload
    @RequestMapping(value = [ "/flow_reload"])
    String flow_reload(@RequestParam(name = "classNameList", required = false) String classNameList) {
        println classNameList
        def ret=flowService.reloadClass(classNameList )
        return JSON.toJSONString(ret)
    }


//    http://localhost:8080/flow_node_reload
    @RequestMapping(value = [ "/flow_node_reload"])
    String flow_node_reload(@RequestParam(name = "classNameList", required = false) String classNameList) {
        println classNameList
        def ret=flowService.reloadNodeClass(classNameList )
        return JSON.toJSONString(ret)
    }

    @RequestMapping(value = ["/direct_run" ])
    String direct_run(
                @RequestParam Map<String,Object> params) {

        return JSON.toJSONString([name:"this is my name"])
    }
}
